package com.cary.camerax.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.UtilsTransActivity;

import java.util.List;

public class FunctionActivity extends AppCompatActivity {
    private final int CAMERA_OK = 1;
    private final int WRITE_EXTERNAL_STORAGE_OK = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        setContentView(R.layout.activity_function);
        // 直接进入相机
       // getPermission();
        CameraActivity.start(this);
    }

    public void getPermission() {
        PermissionUtils.permission(PermissionConstants.STORAGE, PermissionConstants.CAMERA, PermissionConstants.MICROPHONE)
                .rationale(new PermissionUtils.OnRationaleListener() {
                    @Override
                    public void rationale(UtilsTransActivity activity, ShouldRequest shouldRequest) {
                        shouldRequest.again(true);
                    }
                })
                .callback(new PermissionUtils.FullCallback() {
                    @Override
                    public void onGranted(List<String> permissionsGranted) {
                        startActivity(new Intent(FunctionActivity.this, CameraActivity.class));
                    }

                    @Override
                    public void onDenied(List<String> permissionsDeniedForever,
                                         List<String> permissionsDenied) {
                        if (!permissionsDeniedForever.isEmpty()) {
                            PermissionUtils.launchAppDetailsSettings();
                        }
                    }
                }).request();
    }

    public void clickDemo(View v) {
        if (Build.VERSION.SDK_INT > 22) {
            if (ContextCompat.checkSelfPermission(FunctionActivity.this,
                    android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(FunctionActivity.this,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                //先判断有没有权限 ，没有就在这里进行权限的申请
                ActivityCompat.requestPermissions(FunctionActivity.this,
                        new String[]{android.Manifest.permission.CAMERA}, CAMERA_OK);
                ActivityCompat.requestPermissions(FunctionActivity.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_OK);
            } else {
                //说明已经获取到摄像头权限了 想干嘛干嘛
                startActivity(new Intent(this, CameraXDemoActivity.class));
            }
        } else {
            //这个说明系统版本在6.0之下，不需要动态获取权限。
            startActivity(new Intent(this, CameraXDemoActivity.class));
        }
    }
    public void clickCameraX(View v) {
        if (Build.VERSION.SDK_INT > 22) {
            if (ContextCompat.checkSelfPermission(FunctionActivity.this,
                    android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(FunctionActivity.this,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                //先判断有没有权限 ，没有就在这里进行权限的申请
                ActivityCompat.requestPermissions(FunctionActivity.this,
                        new String[]{android.Manifest.permission.CAMERA}, CAMERA_OK);
                ActivityCompat.requestPermissions(FunctionActivity.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_OK);
            } else {
                //说明已经获取到摄像头权限了 想干嘛干嘛
                startActivity(new Intent(this, CameraActivity.class));
            }
        } else {
            //这个说明系统版本在6.0之下，不需要动态获取权限。
            startActivity(new Intent(this, CameraActivity.class));
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case CAMERA_OK:
            case WRITE_EXTERNAL_STORAGE_OK:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //这里已经获取到了摄像头的权限，想干嘛干嘛了可以
                    startActivity(new Intent(this, CameraActivity.class));
                } else {
                    //这里是拒绝给APP摄像头权限，给个提示什么的说明一下都可以。
                    Toast.makeText(FunctionActivity.this, "请手动打开相机、相册权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }

    }
}